﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Web.System.Services.Gateways;
using SaaS.Model.Dto.Gateways;
using Microsoft.AspNetCore.Mvc;
using XCore.Web;
using XCore.Authorization;
using SaaS.Core.Authorization;
using XCore.Domain.Dto.Page;
using XCore.Extensions;
using Microsoft.AspNetCore.Authorization;
using SaaS.Model.Entity.Gateways;

namespace Web.System.Controllers.Clients
{
    /// <summary>
    /// 客户端设置
    /// </summary>
    [Route("api/gateway/[controller]/[action]")]
    [ApiController]
    public class BasicsController : SystemControllerBase
    {
        private readonly BasicsAppService _basicsApplicationService;
        /// <summary>
        /// 构造
        /// </summary>
        public BasicsController(BasicsAppService basicsApplicationService)
        {
            _basicsApplicationService = basicsApplicationService;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission(PermissionNames.Gateway_Basics)]
        public ActionResult List()
        {
            var query = SDC.Basics.AsQueryable();
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.IP,
                c.BasicsSetting.Description,
                c.BasicsSetting.Url,
                c.CreationTime,
                c.Id,
                c.Images,
                c.ReleaseTime,
                c.Sort,
                c.Status,
                c.Tag,
                c.Title,
                c.Type
            });
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpPost]
        //[Authorization(PermissionNames.Gateway_Basics_Create)]
        //public ActionResult Save(BasicsInput input)
        //{
        //    _basicsApplicationService.CreateOrEdit<Basics, BasicsInput>(input);
        //    return Ok(input.Id.HasValue ? ResultFormat.EditSuccess : ResultFormat.CreateSuccess);
        //}
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpDelete]
        //[Authorization(PermissionNames.Gateway_Basics_Delete)]
        //public ActionResult Delete(int id)
        //{
        //    return Ok(_basicsApplicationService.Delete<Basics>(id) ? ResultFormat.DeleteSuccess : ResultFormat.DeleteError);
        //}
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission(PermissionNames.Gateway_Basics_Query)]
        public ActionResult Info(int id)
        {
            return Ok(SDC.Basics.Where(c => c.Id == id).Select(c => new
            {
                c.Id,
                Images = c.Images.ToEnumerable<string>(","),
                c.ReleaseTime,
                c.Sort,
                c.Status,
                c.Tag,
                c.Title,
                c.Type,
            }).FirstOrDefault());
        }
        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Sort(int id, short value)
        {
            return Ok(_basicsApplicationService.Sort(id, value));
        }
    }
}